<!--
 * Copyright © 2023
 * @Author: zhangxiang
 * @Date: 2023-02-14 09:55:42
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-02-14 15:42:55
 * @Description:
 *
-->

<template>
  <div class="detail-port-container">
    <el-row type="flex" align="middle" class="top">
      <el-button
        size="medium"
        type="primary"
        icon="el-icon-plus"
        @click="createClick"
      >添加接口</el-button>
    </el-row>
    <el-table
      v-loading="loading"
      :data="list"
      header-cell-class-name="listHeaderCell"
      :header-cell-style="{ background: '#F3F3F3', color: '#696969' }"
    >
      <el-table-column
        prop="cpId"
        label="接口ID"
        min-width="180"
        show-overflow-tooltip
      />
      <el-table-column
        prop="fixedIps"
        label="固定IP"
        min-width="160"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.fixedIps.join("/") }}
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        prop="type"
        label="类型"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="adminState"
        label="管理状态"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column prop="createDate" label="创建时间" width="170" />
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-popover
            :ref="`popover-delete-${scope.$index}`"
            placement="top"
            width="300"
            style="margin-left: 10px"
          >
            <p>是否确认删除该接口？</p>
            <div style="text-align: right; margin: 0">
              <el-button
                size="mini"
                type="text"
                @click="
                  scope._self.$refs[
                    `popover-delete-${scope.$index}`
                  ].doClose()
                "
              >取消</el-button>
              <el-button
                type="primary"
                size="mini"
                @click="deleteClick(scope)"
              >确定</el-button>
            </div>
            <el-button
              slot="reference"
              type="danger"
              style="height: 22px; line-height: 22px; padding: 0 10px"
              size="mini"
            >删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>

    <!--创建/编辑-->
    <el-dialog
      v-if="showCreateDialog"
      class="dialog"
      :title="'新增接口'"
      :visible="showCreateDialog"
      width="600px"
      top="15vh"
      :close-on-click-modal="false"
      @close="showCreateDialog = false"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="auto">
        <el-form-item label="子网" prop="subnetCpId">
          <el-select
            v-model="form.subnetCpId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in subnetOptions"
              :key="item.cpId"
              :label="item.name + ': ' + item.cidr"
              :value="item.cpId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="IP地址" prop="ipAddress">
          <el-input v-model="form.ipAddress" placeholder="请为创建的实例指定IP地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="showCreateDialog = false">取 消</el-button>
        <el-button
          type="primary"
          size="small"
          :loading="dialogConfirmLoading"
          @click="dialogConfirmClick"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRouterPortList,
  addRouterPort,
  deleteRouterPort,
  getRouterPortSubnets
} from '@/api/cn-resource'

export default {
  name: 'DeatilPort',
  components: {
  },
  mixins: [],
  data() {
    return {
      routerId: '',
      loading: true,
      list: [],
      subnetOptions: [],
      showCreateDialog: false,
      form: {
        subnetCpId: '',
        ipAddress: ''
      }, // 表单
      // 校验规则
      rules: {
        subnetCpId: [{ required: true, message: '请选择子网', trigger: 'change' }]
      },
      dialogConfirmLoading: false
    }
  },
  created() {
    this.routerId = this.$route.query.routerId || ''
    this.getList()
  },
  methods: {
    /**
     * @description: 确定
     * @return {*}
     */
    dialogConfirmClick() {
      // 校验
      this.$refs.form.validate((valid, obj) => {
        if (valid) {
          // 合法
          this.dialogConfirmLoading = true
          addRouterPort({
            routerId: this.routerId,
            ...this.form
          })
            .then((res) => {
              this.$message.success({
                message: '创建路由接口成功！',
                duration: '2000'
              })
              this.showCreateDialog = false // 取消弹窗
              this.getList() // 刷新数据
            })
            .finally(() => {
              this.dialogConfirmLoading = false
            })
        }
      })
    },
    /**
     * @description: 创建
     * @return {*}
     */
    createClick() {
      this.$refs.form && this.$refs.form.resetFields() // 表单重置
      this.showCreateDialog = true // 展示
      getRouterPortSubnets({
        routerId: this.routerId
      }).then(res => {
        this.subnetOptions = Array.isArray(res.data) ? res.data : []
      })
    },
    /**
     * @description: 删除
     * @param {*} row
     * @param {*} index
     * @return {*}
     */
    deleteClick(scope) {
      const row = scope.row
      deleteRouterPort({
        cpId: row.cpId,
        routerId: this.routerId
      }).then((res) => {
        scope._self.$refs[`popover-delete-${scope.$index}`].doClose() // 取消popover
        this.$message.success('删除成功')
        this.getList() // 请求数据
      })
    },
    /**
     * @description: 获取列表
     * @return {*}
     */
    getList() {
      this.loading = true
      getRouterPortList({
        routerId: this.routerId
      })
        .then((res) => {
          this.list = Array.isArray(res.data) ? res.data : []
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.detail-port-container {
  .top {
    margin-bottom: 20px;
    .name {
      .textStyle(14px , 400, #4a4a4a);
      line-height: 20px;
    }
  }
  .el-table::before {
    background-color: #e9e9e9;
  }
  /deep/ .listHeaderCell {
    height: 40px;
    padding: 0;
    box-sizing: border-box;
    &:first-child {
      padding-left: 20px;
    }
  }

  /deep/ .el-table__body {
    .el-table__row td {
      height: 40px;
      padding: 0;
      &:first-child {
        padding-left: 20px;
      }
    }
  }
}
</style>
